﻿using ConfExpert.Datasets;
using DevExpress.XtraEditors;
using DevTeam;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ConfExpert.Forms.Kalite
{
    public partial class Detay : XPopupForm
    {
        public Detay(string DosyaNo)
        {
            InitializeComponent();
            this.DosyaNo = DosyaNo;
        }

        DataSet ds2;
        string DosyaNo;
        BindingSource bs1;
        private void Detay_Load(object sender, EventArgs e)
        {
            ds2 = ExecuteDataSet("GetKaliteKontrol", DosyaNo);
            ds2.Tables[0].PrimaryKey = new DataColumn[] { ds2.Tables[0].Columns["KaliteKontrolId"] };

            

            ds2.Tables[0].TableName = "SayfaNo";
            ds2.Tables[1].TableName = "Sayfalar";
            bs1 = new BindingSource(ds2, "Sayfalar");

            foreach (DataRow r in ds2.Tables[0].Rows)
                listBoxControl1.Items.Add(r["KaliteKontrolId"].ToInt());

            //listBoxControl1.DataBindings.Add("EditValue", bs0, "KaliteKontrolId");
            pictureEdit1.DataBindings.Add("EditValue", bs1, "Dosya");

            pictureEdit1.Properties.ShowZoomSubMenu = DevExpress.Utils.DefaultBoolean.True;
            pictureEdit1.Properties.ShowScrollBars = true;
            pictureEdit1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
            pictureEdit1.KeyDown += Detay_KeyDown;
            this.FormClosing += Detay_FormClosing;
            listBoxControl1.SelectedValueChanged += listBoxControl1_SelectedValueChanged;

            btnSaga.Click += btnSaga_Click;
            btnSola.Click += btnSola_Click;
            btnSil.Click += btnSil_Click;

            btnSaga.Visible = btnSola.Visible = btnSil.Visible =ytk(109);   //Şenol
            listBoxControl1.Focus();
        }

        void listBoxControl1_SelectedValueChanged(object sender, EventArgs e)
        {
            int KaliteKontrolId = listBoxControl1.SelectedValue.ToInt();
            if (KaliteKontrolId > 0)
                bs1.Position = bs1.Find("KaliteKontrolId", KaliteKontrolId);
        }

        void btnSil_Click(object sender, EventArgs e)
        {
            Sil();
        }

        void btnSola_Click(object sender, EventArgs e)
        {
            Sola();
        }

        void btnSaga_Click(object sender, EventArgs e)
        {
            Saga();
        }

        void Detay_FormClosing(object sender, FormClosingEventArgs e)
        {
            ds2.Clear();
            GC.Collect();
        }

        void Detay_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control)
                switch (e.KeyCode)
                {
                    case Keys.D0: Sil(); break;
                    case Keys.Right: Saga(); break;
                    case Keys.Left: Sola(); break;
                }
            //else
            //    switch (e.KeyCode)
            //    {
            //        case Keys.Left: listBoxControl1.SelectedIndex--; break;
            //        case Keys.Right: listBoxControl1.SelectedIndex++; break;
            //    }

        }

        private void Sola()
        {

            int KaliteKontrolId = listBoxControl1.SelectedValue.ToInt();
            if (KaliteKontrolId > 0)
            {
                MyData.xKaliteKontrolRow r = ds.xKaliteKontrol.NewxKaliteKontrolRow();
                r.KaliteKontrolId = KaliteKontrolId;
                Image i2 = pictureEdit1.Image;
                i2.RotateFlip(RotateFlipType.Rotate270FlipNone);
                r.Dosya = ConvertImageToByteArray(i2, ImageFormat.Jpeg);
                ds.xKaliteKontrol.AddxKaliteKontrolRow(r);

                ExecuteNonQuery("UpdKaliteKontrol", ds.xKaliteKontrol);

                pictureEdit1.Image = i2;
                ds.xKaliteKontrol.Clear();
            }
        }

        private void Saga()
        {
            {
                int KaliteKontrolId = listBoxControl1.SelectedValue.ToInt();
                if (KaliteKontrolId > 0)
                {
                    MyData.xKaliteKontrolRow r = ds.xKaliteKontrol.NewxKaliteKontrolRow();
                    r.KaliteKontrolId = KaliteKontrolId;
                    Image i2 = pictureEdit1.Image;
                    i2.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    r.Dosya = ConvertImageToByteArray(i2, ImageFormat.Jpeg);
                    ds.xKaliteKontrol.AddxKaliteKontrolRow(r);

                    ExecuteNonQuery("UpdKaliteKontrol", ds.xKaliteKontrol);

                    pictureEdit1.Image = i2;
                    ds.xKaliteKontrol.Clear();
                }
            }
        }

        private void Sil()
        {
            int KaliteKontrolId = listBoxControl1.SelectedValue.ToInt();
            if (KaliteKontrolId > 0)
                if (XtraMessageBox.Show("Silmek istediğinize Emin misiniz?", "Teyid", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    ExecuteNonQuery("DelKaliteKontrol", KaliteKontrolId);
                    listBoxControl1.Items.Remove(KaliteKontrolId);
                    Notify("silindi.");
                }

        }



        //void ExecQueryTV(string SP, params object[] p)
        //{
        //    using (SqlConnection sqlConn = new SqlConnection(Session.DataManager.ConnectionString))
        //    {
        //        sqlConn.Open();
        //        SqlCommand sqlCmd = sqlConn.CreateCommand();
        //        sqlCmd.CommandText = SP;
        //        sqlCmd.CommandType = CommandType.StoredProcedure;
        //        SqlParameter[] parameters = new SqlParameter[1];
        //        parameters[0] = new SqlParameter("@tbl", SqlDbType.Structured);
        //        parameters[0].Value = p[0];
        //        sqlCmd.Parameters.Add(parameters[0]);
        //        sqlCmd.ExecuteNonQuery();
        //    }
        //}

    }
}
